package 算法.leetcode.str;

import java.util.HashMap;
import java.util.Map;

/**
 * @author lchenglong
 * @date 2022/3/7
 */
public class IntToRoman {

    public static void main(String[] args) {
        String s = intToRoman2(58);
        System.out.println(s);
    }

    public static String intToRoman(int num) {
        initMap();
        StringBuilder stringBuilder = new StringBuilder();
        char q = (char) (num / 1000);
        while (q > 0) {
            stringBuilder.append("M");
            q--;
        }
        num = num % 1000;
        char b = (char) (num / 100);
        if (b < 4) {
            while (b > 0) {
                stringBuilder.append("C");
                b--;
            }
        } else if (b == 4) {
            stringBuilder.append("CD");
        } else if (b == 5) {
            stringBuilder.append("D");
        } else if (b < 9) {
            b = (char) (b - 5);
            stringBuilder.append("D");
            while (b > 0) {
                stringBuilder.append("C");
                b--;
            }
        } else if (b == 9) {
            stringBuilder.append("CM");
        }
        num = num % 100;
        b = (char) (num / 10);
        if (b < 4) {
            while (b > 0) {
                stringBuilder.append("X");
                b--;
            }
        } else if (b == 4) {
            stringBuilder.append("XL");
        } else if (b == 5) {
            stringBuilder.append("L");
        } else if (b < 9) {
            b = (char) (b - 5);
            stringBuilder.append("L");
            while (b > 0) {
                stringBuilder.append("X");
                b--;
            }
        } else if (b == 9) {
            stringBuilder.append("XC");
        }

        b = (char) (num % 10);
        if (b < 4) {
            while (b > 0) {
                stringBuilder.append("I");
                b--;
            }
        } else if (b == 4) {
            stringBuilder.append("IV");
        } else if (b == 5) {
            stringBuilder.append("V");
        } else if (b < 9) {
            b = (char) (b - 5);
            stringBuilder.append("V");
            while (b > 0) {
                stringBuilder.append("I");
                b--;
            }
        } else if (b == 9) {
            stringBuilder.append("IX");
        }

        return stringBuilder.toString();

    }

    public static String intToRoman2(int num){
        initMap();
        StringBuilder stringBuilder = new StringBuilder();
        while (num>0) {
            double ws = Math.floor(Math.log10(num));
            int h = (int) ws;
            // 取商
            int v = (int) (num / Math.pow(10, h));
            String value = map.get(h);
            String[] valueArray = value.split(",");
            if (v < 4) {
                while (v > 0) {
                    stringBuilder.append(valueArray[0]);
                    v--;
                }
            } else if (v == 4) {
                stringBuilder.append(valueArray[0]).append(valueArray[1]);
            } else if (v == 5) {
                stringBuilder.append(valueArray[1]);
            } else if (v < 9) {
                v = (char) (v - 5);
                stringBuilder.append(valueArray[1]);
                while (v > 0) {
                    stringBuilder.append(valueArray[0]);
                    v--;
                }
            } else if (v == 9) {
                stringBuilder.append(valueArray[0]).append(valueArray[2]);
            }
            num = (int) (num % Math.pow(10,h));
        }
        return stringBuilder.toString();
    }

    static Map<Integer, String> map = new HashMap();

    private static Map<Integer, String> initMap() {
        map.put(0, "I,V,X");
        map.put(1, "X,L,C");
        map.put(2, "C,D,M");
        map.put(3, "M,P,Q");
        return map;
    }

}
